hackerrank

Learn about hackerrank, we have the largest and most updated hackerrank information on alibabacloud.com

Hackerrank-"Sherlock and GCD"

This is a very smart observation:http://www.martinkysel.com/hackerrank-sherlock-and-gcd-solution/#http://www.martinkysel.com/hackerrank-sherlock-and-gcd-solution/# fromFunctoolsImportReducedefgcd (A, b):ifA = = 1orb = = 1: return1ifA% b = =0:returnbreturnGCD (b, a%b)defMULTIPLE_GCD (arr):returnReduceLambdax,y:gcd (x, y), arr)#################T =int (input ()) forIinchRange (0,t): N=int (input ()) Arr

Hackerrank yesterday to participate in a contest summary

Yesterday for the first time to participate in a Hackerrank game, from 11 o'clock Noon to do the night 12 o'clock, finally too tired, at the end of the game one hour to go to bed. Completely passed the first two questions, the third question for a sentence has not been correctly understood, so did not pass the majority of test case, the final ranking in the contest in 23**/60**. Oneself is holding the mentality of study to go, although this achievemen

Hackerrank "The Indian Job"

A Sly knapsack problem in disguise! Thanks to Https://github.com/bhajunsingh/programming-challanges/tree/master/hackerrank/algorithms/the-indian-jobLesson learnt: The Italian\indian job is two-way knapsack. And some complext problem can be converted to a simpler one.Code is amazingly simple:#include #include#include#include#includeusing namespacestd;intKnapsack (vectorint> a,intG) { intn =a.size (); Vectorint> dp (G +1); for(inti =1; I ) for(

Hackerrank-"String transmission"

Classic and challenging dp! And you need combine several tricks together with DP-make it 100% pass.My Main reference is here:https://github.com/havelessbemore/hackerrank/blob/master/algorithms/bit_manipulation/ String-transmission.javaBasically it is a counting problem.It's easy-to-possible count should be SUM (CR (n, 0), CR (n, 1),.. Cr (n, k)) without the limitation of "No Periodic strings". And then we can count how many "periodic" strings there is

Bonetrousle Hackerrank Math + thinking problem

= 1e5 + -; LL A[MAXN];voidWork () {//Ios::sync_with_stdio (false);LL N, K, B;//scanf ("%lld%lld%lld", n, k, b);CIN >> N >> k >>b; //cout LL mi =0, mx =0; for(inti =1; I i) {mi+=i; } for(LL i = k; I >= K-b +1; --i) {mx+=i; //if (MX if(MX >= N) Break;//OK } //cout if(MX N) {printf ("-1\n"); return ; } if(Mi >N) {printf ("-1\n"); return ; } LL Now=mi; intto =b; LLGet=K; for(inti =1; I i; while(true) { now= Now-a[to] +Get; A[to]=Get; if(Now >=N) {LL cut= Now-N; A[to]-=cu

Hackerrank -- mixing proteins (math)

first protein. Using this data, they have written a small simulation software to get mutations second by second. the problem is that the protein rings can be very long (up to 1 million proteins in a single ring) and they want to know the state of the ring after upto 109 seconds. thus their software takes too long to report the results. they ask you for your help. Input FormatInput contains 2 lines.First line has 2 integers n and k, n being the length of the protein ring and K the desired numb

Hackerrank -- Ashton and string (suffix array)

Question Link Ashton appeared for a job interview and is asked the following question. arrange all the distinct substrings of a given string in lexicographical order and concatenate them. print the kth character of the concatenated string. it is assured that given value of K will be valid I. e. there will be a kth character. can you help Ashton out with this? Input FormatFirst line will contain a number t I. e. Number of test cases.First line of each test case will contain a string containing

Similar Pair _ HackerRank, pairhackerrank

Similar Pair _ HackerRank, pairhackerrank Hacker rank is really more difficult than leetcode .. This question is a bit clever .. Each path is searched in depth and then enumerated. Most people think about it, but the key is that this definitely times out. The trick is to create a line segment tree for each path to accelerate the query. The complexity of each similar query changes from O (h) to O (lgh ).. Two mistakes were made. (1) Use long to store

Hackerrank-Sherlock and the beast

Greedy beats DP this time... I tried several DP solutions first, but all failed with re \ TLE. If you 'feel' the problem, greedy shocould be working: (A solution from discussion) Def getiterator (n): While n> 0: If n % 3 = 0: break; else: N-= 5 return n t = input () for I in xrange (t): n = int (input () rows = getrows (n) If rows Hackerrank-Sherlock and the beast

Hackerrank "Fair rations"

Another fun greedy problem to work on:we simply go from first to second last person, as long someone is odd, we distribut E bread to she and her next.#include #includeusing namespacestd;intMain () {intN; CIN>>N; Vectorint>B (N); for(intB_i =0; B_i ) {cin>>B[b_i]; } intCNT =0; for(inti =0; I 1; i + +) { if(B[i]%2) {B[i]++; B[i+1]++; CNT+=2; } } if(B.back ()%2) cout"NO"Endl; ElsecoutEndl; return 0;}Hackerrank "Fair rations"

Hackerrank "Prim ' s (MST): Special subtree"

An intuitive Prim algorithm impl.#include #include#include#include#includeusing namespacestd;structedge{Edge (): s (0), T (0), D (0{} Edge (unsigned RS, unsigned rt, unsigned rd): s (rs), T (RT), D (RD) {} unsigned s; unsigned t; unsigned D; BOOL operator()(ConstEdge e1,ConstEdge E2) { returne1.d >e2.d; }};intMain () {LongN, M; CIN >> N >>m; //From -to-and (length, id)unordered_mapG; for(inti =0; I ) {Unsigned A, B, D; CIN>> a >> b >>D; G[A][B]= G[b][a] =D; } unsigned s; CIN>>s; Un

Hackerrank-insertion Sort Advanced Analysis

"How many inverted pairs"-so usually ends up with mergesort solution (of course there is other solutions out there)defmergesort (arr):ifLen (arr) = = 1: return0, arr mid= Len (arr)//2Cnt1, arr1=mergesort (Arr[:mid]) Cnt2, arr2=mergesort (arr[mid:])#RET =[] CNT=0 I=0 J=0 Inx=0 whileI andJ Len (arr2):ifArr1[i] Arr2[j]: Ret.append (arr1[i]) I+ = 1elifARR2[J] Arr1[i]: Ret.append (arr2[j]) CNT+ = Len (arr1)-I#arr2-arr1J + = 1 whileI Len (arr1): Ret.append (Arr1[i]) I+ = 1 whileJ Len (arr2): R

Hackerrank "Larry's Array"

I caught the sparkle in my mind and got AC1! It is a great great experience!So the basic Idea:permute on 3 consecutive items doesn ' t change the parity of the No. of inversions. Each permutation can is only remove 0 or 2 inversions. So we say "YES" when No. of inversion% 2 = = 0. And we use MergeSort to count it in O (NLGN).RET =0defmerge (arr1, arr2):Globalretif notARR1:returnarr2if notARR2:returnarr1 forV2inchArr2:arr1.append (v2) I= Len (arr1)-1 while(I >0):ifArr1[i] ]: Arr1[i-1],arr1[i] =

Hackerrank-common Child

Longest Common subsequence in disguise. Python Impl. Have TLE in one case. and C + + is fine.#include #include#include#include#includestring>#include#includeusing namespacestd;intMain () {stringA, B; CIN>>A; CIN>>b; size_t Len=a.length (); Vectorint>> DP (len +1, vectorint> (len +1,0)); for(inti =0; I ) for(intj =0; J ) { if(I * j = =0) Dp[i][j] =0; Else if(A[i-1] = = B[j-1]) Dp[i][j]= Dp[i-1][j-1] +1; ElseDp[i][j]= Max (Dp[i-1][J], Dp[i][j-1]); } coutDp[len][len]; return 0;}

[Hackerrank] The longest Common subsequence

This is the classic LCS problem. Since it requires you-to-print one longest common subsequence, just use the O (m*n)-space version here.My accepted code is as follows.1#include 2#include 3#include 4 5 using namespacestd;6 7vectorint> LCS (vectorint> A, vectorint>b) {8 intn = a.size (), M =b.size ();9vectorint> > DP (n +1, vectorint> (M +1,0));Ten for(inti =1; I ) { One for(intj =1; J ) { A if(A[i-1] = = B[j-1]) Dp[i][j] = dp[i-1][j-1] +1; - ElseDP[I][J]

Make the most (Hackerrank Codeagon)

actually an extension of the scheduling problem. That is, given n meetings, each meeting has a start time and an end time, and there are m individuals who ask the M person how many meetings can be attended by up to a total.If m=1, of course, according to the end time sort, can.Here, simply deform, set b[j] for the point where the J-person is currently located (initially 0), for the first meeting, if there are more than one person to meet the criteria, select B[j] the largest one.Attached code:1

hackerrank# Hexagonal Grid

]) { - if(!U[i])WuyiCAN[I][J] |= Can[i +1][j +2]; the if(U[i] u[i +1]) -CAN[I][J] |= Can[i +2][j +2]; Wu } - } About } $ if(-I. >=0) { - if(!U[i]) { -CAN[I][J] = can[i +1][j]; -}Else { A if(D[j]) +CAN[I][J] |= Can[i +1][j +1]; the if(U[i +1]) { - if(!D[j]) $CAN[I][J] |= Can[i +2][j +1]; the

Hackerrank-"The longest increasing subsequence"

It requires O (NLGN) solution. And actually I think the passing code is for "Non-decreasing".#include #include#include#include#includestring>#include#includeusing namespacestd;intMain () {intT CIN >>T; Vectorint>arr (t); for(inti =0; I ) Cin>>Arr[i]; Vectorint> dp (t,1); //C[i]: Smallest last elem of a LIS seq with lenghth ivectorint> C (1, arr[0]); intRET =1; for(inti =1; I ) { if(Arr[i] 0]) {c[0] =Arr[i]; Dp[i]=1; } Else if(Arr[i] >=C.back ()) {C.push_back (arr[i]

Hackerrank-"Stock maximize"

; Vectorinch(n); REP (i,0, N) Cin >>inch[i]; coutinch) Endl; } return 0;}But all TLE. So there was must be a O (n) solution, and there is. What's better than a standard DP in cerntain cases? Greedy.#include #include#include#includeusing namespacestd;#defineREP (i, S, N) for (int i = s; i typedefLong LongLL; LL Calc (Vectorinch) {size_t len=inch. Size (); LL ret=0; Std::reverse (inch. Begin (),inch. End ()); LL Peak= -1; REP (i,0, Len) { if(inch[I] >Peak) {Peak=inch[i]; } Else{re

Hackerrank-"Red John is Back"

1AC. 1D DP + sieving#include #include#include#include#include#include#includestring>#include#include#include#includeusing namespacestd;intWaysintN) { if(N 1)returnN; Vectorint> dp (n +1,0); dp[0] =1; for(inti =0; I ) {Dp[i+1] +=Dp[i]; if((i +4) 4] +=Dp[i]; } returndp[n];}intCntprimes (intN//sieving{vectorint> Mark (N,1); mark[0] =0; intInx =1;//2 intSCNT =1; while(Inx 0) {scnt=0; for(inti =2* Inx +1; I 1) ) {Mark[i]=0; SCNT++; } if(Scnt >0) {Inx++; while(Inx 0) inx++; } }

Total Pages: 5 1 2 3 4 5 Go to: Go

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.